package com.ali.juejin.model.main

import androidx.lifecycle.MutableLiveData
import com.ali.juejin.base.BaseModelView
import com.ali.juejin.bean.v1.DateTag
import com.ali.juejin.bean.v1.PageLimit
import com.ali.juejin.services.main.HomeRecommendServices
import com.ali.juejin.utils.Request
import com.google.gson.Gson
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import com.ali.juejin.bean.v1.RecommendBean
import com.ali.juejin.config.UrlConfigV1

class RecommendModel: BaseModelView() {
    val homeRecommendList by lazy { MutableLiveData<RecommendBean>() }
    val homeRecommendHotList by lazy { MutableLiveData<RecommendBean>() }
    val net by lazy { Request.instance(UrlConfigV1.RECOMMENDAPI) }

    val selectedTag by lazy { MutableLiveData<DateTag>(DateTag(3,"3日内")) }


    fun getHomeRecommendHotList(){
        val pageLimit = PageLimit(2606,"eyJ2IjoiNjg1NjQxOTUwMTc3Nzg0NjI3OSIsImkiOjZ9",2,3,213)
        val info = Gson().toJson(pageLimit)
        val body = RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(),info)
        request<RecommendBean>({
            val data = net.create(HomeRecommendServices::class.java).getInfoList(body)
            homeRecommendHotList.postValue(data)
        })
    }

    fun getHomeRecommendList(){
        val pageLimit = PageLimit(2606,"0",2,20,3)
        val info = Gson().toJson(pageLimit)
        val body = RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(),info)
        request<RecommendBean>({
            val data = net.create(HomeRecommendServices::class.java).getInfoList(body)
            homeRecommendList.postValue(data)
        })
    }

    fun getMoreHomeRecommendList(){
        val pageLimit = homeRecommendList.value?.cursor?.let { PageLimit(2606, it,2,20,3) }
        val info = Gson().toJson(pageLimit)
        val body = RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(),info)
        request<RecommendBean>({
            val data = net.create(HomeRecommendServices::class.java).getInfoList(body)
            val tempArticleList = homeRecommendList.value?.data
            tempArticleList?.let { data.data.addAll(it) }
            homeRecommendList.postValue(data)
        })
    }

}